/**
 * adx-common#com.baidu.ub.generic.bo.BaseObject.java
 * 下午6:46:03 created by Darwin(Tianxin)
 */
package com.unbiz.common.lang;

import java.io.Serializable;

import org.apache.commons.collections4.KeyValue;

import com.unbiz.common.apache.ToStringBuilder;
import com.unbiz.common.apache.ToStringStyle;

/**
 * 使用GenericDao做映射的对象，必须都继承与这个类
 * 
 * @author Darwin(Tianxin)
 */
public class BaseObject<KEY extends Comparable> implements KeyValue<KEY,Object>,Serializable,Comparable<BaseObject<KEY>> {

    private static final long serialVersionUID = 1L;

    public BaseObject() {
    }
    
    public BaseObject(KEY id) {
    	this.id = id;
    }

    /**
     * 主键字段
     */
    protected KEY id;       
  

    @Override
	public KEY getKey() {		
		return id;
	}


    public void setKey(KEY id) {
        this.id = id;
    }
    
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    /**
	 * Returns if the {@code Persistable} is new or was persisted already.
	 * 
	 * @return if the object is new
	 */
	public boolean isNew(){
		return id==null;
	}
	
	@Override
	public Object getValue() {
		// TODO Auto-generated method stub
		return this;
	}
	
	public boolean equals(Object other) {
        return (other instanceof BaseObject) && equals((BaseObject) other);
    }

    /**
     * 判断本货币对象与另一货币对象是否相等。
     * 
     * <p>
     * 本货币对象与另一货币对象相等的充分必要条件是：<br>
     * <ul>
     * <li>金额相同。
     * <li>币种相同。
     * </ul>
     * 
     * @param other 待比较的另一货币对象。
     * @return <code>true</code>表示相等，<code>false</code>表示不相等。
     */
    public boolean equals(BaseObject other) {
        return this.getKey().equals(other.getKey()) && (this.getValue() == other.getValue());
    }

    /**
     * 计算本货币对象的杂凑值。
     * 
     * @return 本货币对象的杂凑值。
     * 
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        return this.getKey().hashCode();
    }
	
	@Override
	public int compareTo(BaseObject<KEY> other) {
		return getKey().compareTo(other.getKey());
	}
}
